package com.iha.ponggame;

import javax.microedition.khronos.opengles.GL10;

import com.iha.ponggame.framework.Game;
import com.iha.ponggame.framework.Screen;
import com.iha.ponggame.framework.gl.FPSCounter;
import com.iha.ponggame.framework.gl.Texture;
import com.iha.ponggame.framework.gl.Vertices;
import com.iha.ponggame.framework.impl.GLGame;
import com.iha.ponggame.framework.impl.GLGraphics;

public class Ponggame extends GLGame {

	@Override
	public Screen getStartScreen() {
		return new BobScreen(this);
	}
	
	class BobScreen extends Screen {
        GLGraphics glGraphics;
        Texture padTexture;
        Texture ballTexture;
        Vertices bobModel;
        Ball ball;
        Pad topPad;
        Pad bottomPad;
        FPSCounter fpsCounter;
        
        
        public BobScreen(Game game) {
            super(game);
            glGraphics = ((GLGame)game).getGLGraphics();
            
            padTexture = new Texture((GLGame)game, "mars-bar.jpg");
            ballTexture = new Texture((GLGame)game, "ball.png");
            
            bobModel = new Vertices(glGraphics, 4, 12, false, true);
            bobModel.setVertices(new float[] { -16, -16, 0, 1,  
                                                16, -16, 1, 1, 
                                                16,  16, 1, 0,
                                               -16,  16, 0, 0, }, 0, 16);
            bobModel.setIndices(new short[] {0, 1, 2, 2, 3, 0}, 0, 6);

            topPad = new Pad(true);
            bottomPad = new Pad(false);
            ball = new Ball();
            
            fpsCounter = new FPSCounter();
        }
        
        @Override
        public void update(float deltaTime) {         
            game.getInput().getKeyEvents();
            
        	topPad.dirX = -game.getInput().getAccelX() * 100;
        	topPad.update(deltaTime);
        	bottomPad.dirX = -game.getInput().getAccelX() * 100;
        	bottomPad.update(deltaTime);
        	ball.update(deltaTime);
        }

        @Override
        public void present(float deltaTime) {
            GL10 gl = glGraphics.getGL();
            
            
            gl.glEnable(GL10.GL_TEXTURE_2D);
            gl.glClearColor(1,1,1,0);
            gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glOrthof(0, 320, 0, 480, 1, -1);
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
            
                        
            padTexture.bind();
            gl.glMatrixMode(GL10.GL_MODELVIEW);
            gl.glEnable(GL10.GL_BLEND);
            gl.glEnable(GL10.GL_ALPHA);
            gl.glLoadIdentity();
            gl.glTranslatef(topPad.x, topPad.y, 0);
            bobModel.draw(GL10.GL_TRIANGLES, 0, 6);

          
            gl.glLoadIdentity();
            gl.glTranslatef(bottomPad.x, bottomPad.y, 0);
            bobModel.draw(GL10.GL_TRIANGLES, 0, 6);
            
            ballTexture.bind();
            gl.glLoadIdentity();
            gl.glTranslatef(ball.x, ball.y, 0);
            bobModel.draw(GL10.GL_TRIANGLES, 0, 6);
            
            fpsCounter.logFrame();
        }
        
        @Override
        public void pause() {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void resume() {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void dispose() {
            // TODO Auto-generated method stub
            
        }
	}
}
